#!/bin/bash

# 检查是否具有 root 权限
if [ "$EUID" -ne 0 ]; then
    echo "请使用 root 权限运行此脚本"
    exit 1
fi

# 安装 CSF 依赖
yum install -y epel-release unzip

# 下载 CSF
wget https://download.configserver.com/csf.tgz

# 解压文件
tar -xzf csf.tgz
cd csf

# 安装 CSF
sh install.sh

config_changes=(
    "TESTING = \"0\""
    "PROXY_CHECK = \"1\""
    "LF_NETBLOCK_RATE = \"1\""
)

# 遍历配置项，检查是否存在，存在则修改，不存在则添加
for change in "${config_changes[@]}"; do
    config_key=$(echo "$change" | awk -F' = ' '{print $1}')
    grep -q "^$config_key" /etc/csf/csf.conf && \
        sed -i "s/^$config_key.*/$change/" /etc/csf/csf.conf || \
        echo "$change" >> /etc/csf/csf.conf
done


# 启动 CSF
csf -r

# 设置开机自启动
systemctl enable csf
systemctl enable lfd

# 启动 CSF 服务
systemctl start csf
systemctl start lfd

# 输出安装完成信息
echo "CSF 安装完成，请检查是否有错误信息。"

